import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 1805. 字符串中不同整数的数目
 * https://leetcode-cn.com/problems/number-of-different-integers-in-a-string/
 */
public class Solutions_1805 {
    public static void main(String[] args) {
        String word1 = "a123bc34d8ef34";  // output: 3
        String word2 = "leet1234code234";  // output: 2
        String word3 = "a1b01c001";  // output: 1
        String word4 = "035985750011523523129774573439111590559325a1554234973";  // output: 2
        String word5 = "ubhb4xkon7yxlr0007c";  // output: 2

        List<String> inputs = new ArrayList<>();
//        inputs.add(word1);
//        inputs.add(word2);
//        inputs.add(word3);
//        inputs.add(word4);
        inputs.add(word5);

        for (String input : inputs) {
            int result = numDifferentIntegers(input);
            System.out.println(result);
        }
    }

    public static int numDifferentIntegers(String word) {
        char[] arr = word.toCharArray();
        // 使用 set 集合达到字符串去重的效果
        Set<String> set = new HashSet<>();

        for (int i = 0; i < arr.length; i++) {
            if (isNum(arr[i])) {
                // 向后找到连续的数字
                int next = i + 1;
                while (next < arr.length && isNum(arr[next])) {
                    next ++;
                }
                // 排除连续数字中的前导零
                int left = i;
                while (left < next && arr[left] == '0') {
                    left ++;
                }
                if (left == next) {
                    // "0000" 的情况
                    set.add(String.valueOf(0));
                } else {
                    String str = word.substring(left, next);
                    set.add(str);
                }
                i = next;
            }
        }
        return set.size();
    }

    public static boolean isNum(char c) {
         return c >= '0' && c <= '9';
    }
}
